define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'track/code/index' + location.search,
                    add_url: 'track/code/add',
                    edit_url: 'track/code/edit',
                    del_url: 'track/code/del',
                    multi_url: 'track/code/multi',
                    import_url: 'track/code/import',
                    table: 'track_code',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'weigh',
                search:false,//隐藏默认搜素框
                showToggle: false,//浏览模式可以切换卡片视图和表格视图两种模式
                showColumns: false,//显示隐藏列可以快速切换字段列的显示和隐藏
                showExport: false,//默认导出功能
                searchFormVisible: true,//默认弹出搜索框
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: 'BETWEEN'},
                        {field: 'batch', title: __('Batch'),formatter:Table.api.formatter.search},
                        {field: 'batch2', title: __('全批次号')},
                        {field: 'number', title: __('Number')},
                        {field: 'qrcode', title: __('二维码'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'qrcode',
                                    title: __('防伪二维码'),
                                    classname: 'btn btn-xs btn-click btn-success',
                                    icon: 'fa fa-qrcode',
                                    click:function (data,row) {
                                        layer.open({
                                            type: 1,
                                            title:'防伪码:'+row.number,
                                            closeBtn: 1, //不显示关闭按钮
                                            shadeClose: true,
                                            area: ['350px', '500px'], //宽高
                                            content: '<div style="text-align: center"><img style="-webkit-user-select: none;" src="'+row.qrcode+'" alt=""><br><b>防伪二维码预览图</b><br><br><p style="color:red">实际应用请导出数据，印刷厂或喷码机<br>会自动印刷成二维码图形。</p></div>',
                                        });
                                    }
                                },
                            ],
                            formatter: Table.api.formatter.buttons,
                            operate: false
                        },
                        {field: 'product.name', title: __('Product_id'),operate: 'LIKE',formatter:Table.api.formatter.search,cellStyle: {css: {"max-width": "200px","white-space":"nowrap","overflow":"hidden","text-overflow":"ellipsis"}}},
                        {field: 'agent.nickname', title: __('Agent_id'),operate: 'LIKE'},
                        {field: 'find_num', title: __('Find_num')},
                        {field: 'weigh', title: __('Weigh'), operate: false},
                        {field: 'switch', title: __('Switch'), searchList: {"1":__('Yes'),"0":__('No')}, table: table, formatter: Table.api.formatter.toggle},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //取消双击编辑
            table.off('dbl-click-row.bs.table');
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'track/code/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'track/code/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'track/code/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        edits: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                //选择修改条件
                $(document).on('change', "select[name='row[edit_where]']", function () {
                    var that = this;
                    var value = $(that).val();
                    if(value>0) {
                        if(value == 1){
                            $('.edit_product_id').show();
                        }else{
                            $('.edit_product_id').hide();
                        }
                        if(value == 2){
                            $('.edit_agent_id').show();
                        }else{
                            $('.edit_agent_id').hide();
                        }
                        if(value == 3){
                            $('.edit_batch').show();
                        }else{
                            $('.edit_batch').hide();
                        }

                    } else {
                        $('.edit_product_id').hide();
                        $('.edit_agent_id').hide();
                        $('.edit_batch').hide();
                    }
                    return false;
                });
            }
        }
    };

    var submitForm = function (ids, layero) {
        var table = $("#table");
        var options = table.bootstrapTable('getOptions');
        var search = options.queryParams({});
        $("input[name=ids]", layero).val(ids);
        $("input[name=filter]", layero).val(search.filter);
        $("input[name=op]", layero).val(search.op);
        $("form", layero)[0].submit();
    };
    $(document).on("click", ".btn-export", function () {
        var table = $("#table");
        var ids = Table.api.selectedids(table);
        var page = table.bootstrapTable('getData');
        var all = table.bootstrapTable('getOptions').totalRows;
        Layer.confirm("请选择导出的选项<form action='" + Fast.api.fixurl("track/code/export") + "' method='post' target='_blank'><input type='hidden' name='ids' value='' /><input type='hidden' name='filter' ><input type='hidden' name='op'><input type='hidden' name='search'><input type='hidden' name='columns'></form>", {
            title: '导出数据',
            btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
            success: function (layero, index) {
                $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
            }
            , yes: function (index, layero) {
                submitForm(ids.join(","), layero);
                Layer.close(index);
                return false;
            }
            ,
            btn2: function (index, layero) {
                var ids = [];
                $.each(page, function (i, j) {
                    ids.push(j.id);
                });
                submitForm(ids.join(","), layero);
                Layer.close(index);
                return false;
            }
            ,
            btn3: function (index, layero) {
                submitForm("all", layero);
                Layer.close(index);
                return false;
            }
        })
    });
    return Controller;
});